package com.hs.pre.page;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

public class PageParam {
	
	private HttpServletRequest request;
	 /**
     * 基本路径
     * @return
     */
    private String basePath = "";
    
    /**
     * 标识
     * 如果大于0则表示用已有基本sql 否则用传入参数
     * @return
     */
    private int identifier = -1 ;
    

    /**
	 * 基础sql
	 * 
	 */
	private String baseSql = null;
	
	/**
	 * 基础参数
	 */
	private Object[] baseParam ;
	
	/***
	 * 每一页显示的条目数 默认为10
	 */
	private int pageSize = 10;
	
	/**
	 * 当前页的页码 默认为1
	 */
	private int thisPageNumber = 1;
	
	/**
	 * 需要传递的参数
	 */
	private Map paramMap = new HashMap();
    
	public PageParam(HttpServletRequest request)
	{
		this.request = request;
		init();
	}
	
	/**
	 * 分页初始化
	 */
	public void init()
	{
		if(request == null)
			return;
		try {
			String pageSizeTem = request.getParameter("pageSize");
			String thisPageNumberTem = request.getParameter("pageNumber");
			
			String baseSql = request.getParameter("baseSql");
			String identifier = request.getParameter("identifier");
			String baseParamTem = request.getParameter("baseParam");
			if(identifier != null && !(identifier = identifier.trim()).equals(""))
			{
				this.setBaseSql(baseSql);
				this.identifier = Integer.parseInt(identifier);
				if(request.getParameter("baseParam") != null && !(baseParamTem = baseParamTem.trim()).equals(""))
					baseParam = baseParamTem.split(",");
			
				if(thisPageNumberTem != null && !(thisPageNumberTem.trim()).equals("")
						&& pageSizeTem != null && !
						(pageSizeTem.trim()).equals(""))
				{
					this.setPageSize(Integer.parseInt(pageSizeTem));
					this.setThisPageNumber(Integer.parseInt(thisPageNumberTem));
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public String getBasePath() 
	{
		return basePath;
	}

	public void setBasePath(String basePath) {
		this.basePath = basePath;
	}

	public boolean isIdentifier() {
		return (identifier == 1000);
	}
	public String getBaseSql() {
		return baseSql;
	}

	public void setBaseSql(String baseSql) {
		this.baseSql = baseSql;
	}

	public Object[] getBaseParam() {
		return baseParam;
	}

	public void setBaseParam(Object[] baseParam) {
		this.baseParam = baseParam;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getThisPageNumber() {
		return thisPageNumber;
	}

	public void setThisPageNumber(int thisPageNumber) {
		this.thisPageNumber = thisPageNumber;
	}

	public Map getParamMap() {
		return paramMap;
	}

	public void setParamMap(Map paramMap) {
		this.paramMap = paramMap;
	}
}
